package com.sportradar.assessment.work.services;

import java.util.Date;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ThreadManager {

	private NumberProgression numberProgression;
	private Date lastUpdate;
	private ReentrantReadWriteLock readWriteLock= new ReentrantReadWriteLock(true);
	private final Lock read  = readWriteLock.readLock();
	private final Lock write  = readWriteLock.writeLock();
	
	private String currentEnabledThreadName;
	private long intervalBetweenCalculations;
		
	public long getIntervalBetweenCalculations() {
		return intervalBetweenCalculations;
	}

	public void setIntervalBetweenCalculations(long intervalBetweenCalculations) {
		this.intervalBetweenCalculations = intervalBetweenCalculations;
	}

	public ThreadManager(NumberProgression numberProgression,String currentEnabledThreadName) {	
		this.numberProgression = numberProgression;
		this.currentEnabledThreadName = currentEnabledThreadName;
		this.lastUpdate= new Date();
	}

	public Date getLastUpdate() {
		return lastUpdate;
	}

	public void setLastUpdate(Date lastUpdate) {
		this.lastUpdate = lastUpdate;
		
	}

	public void setCurrentEnabledThread(String threadName){

		System.out.println("TRYING TO CHANGE CURRENT THREAD");
		write.lock();
		try {
			System.out.println("MODIFIED CURRENT THREAD");
			currentEnabledThreadName = threadName;
			notifyAll();
		} finally {
			write.unlock();
			System.out.println("MODIFIED CURRENT THREAD UNLOCK!!!");
			
		}
	}
	
	
	public void executeCalculation(String threadName) throws InterruptedException{
		
		read.lock();
		try {
			if(!threadName.equals(currentEnabledThreadName)){
				System.out.println("ThreadName "+threadName+"is going to wait...");
				wait();
				System.out.println("ThreadName"+threadName+"out from wait!!");
			}
			TimeUnit.SECONDS.sleep(intervalBetweenCalculations);
			long nextNumber=numberProgression.getNextNumber();
			System.out.println(threadName+ ":"+nextNumber);
		} finally {
			read.unlock();
		}
		/*
		while(!threadName.equals(currentEnabledThreadName)){
			System.out.println("ThreadName "+threadName+"is going to wait...");
				wait();
				System.out.println("ThreadName"+threadName+"out from wait!!");
		}*/
		
//		TimeUnit.SECONDS.sleep(intervalBetweenCalculations);
//		long nextNumber=numberProgression.getNextNumber();
//		System.out.println(threadName+ ":"+nextNumber);
//		notifyAll();
	}
	
	
	/*
	private String currentEnabledThreadName;
	private long computationTimeIntervalThread1;
	private long computationTimeIntervalThread2;
	
	
	
	

	
	public long getComputationTimeIntervalThread1() {
		return computationTimeIntervalThread1;
	}



	public void setComputationTimeIntervalThread1(
			long computationTimeIntervalThread1) {
		this.computationTimeIntervalThread1 = computationTimeIntervalThread1;
	}



	public long getComputationTimeIntervalThread2() {
		return computationTimeIntervalThread2;
	}



	public void setComputationTimeIntervalThread2(
			long computationTimeIntervalThread2) {
		this.computationTimeIntervalThread2 = computationTimeIntervalThread2;
	}



	public boolean isThreadAllowedToContinue(String threadName){
		return  threadName.equals(currentEnabledThreadName);
	}
	
	 */
	
}
